package com.conoic.sxs.xcj.gateway.common.util.string;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class XcjStringUtil {

    /**
     * 手机号验证
     */
    private static final Pattern DEFAULT_PATTERN = Pattern.compile("^1\\d{10}$");

    /**
     * Check mobile boolean.
     * 判断手机号输入是否合法
     *
     * @param mobile the mobile
     * @return the boolean
     */
    public static boolean mobileFormatCheck(String mobile) {
        return mobileFormatCheck(mobile, null);
    }

    /**
     * Check mobile boolean.
     * 判断手机号输入是否合法
     *
     * @param mobile  the mobile
     * @param pattern 正则表达式
     * @return the boolean
     */
    public static boolean mobileFormatCheck(String mobile, String pattern) {
        Pattern pat = DEFAULT_PATTERN;
        if (isNotNull(pattern)) {
            pat = Pattern.compile(pattern);
        }
        Matcher m = pat.matcher(mobile);
        return isNotNull(mobile) && m.matches();
    }

    public static Boolean isNull(String s) {

        return StringUtils.isBlank(s) || "null".equalsIgnoreCase(s);
    }

    public static Boolean isNotNull(String s) {
        return !isNull(s);
    }

    /**
     * Gets suffix.获取文件的后缀名(注：a.jpg -> jpg)
     *
     * @param fileName the str
     * @return the suffix
     */
    public static String getSuffix(String fileName) {
        String result = null;
        if (StringUtils.isNotBlank(fileName)) {
            result = StringUtils.substring(fileName, fileName.lastIndexOf('.') + 1, fileName.length());
        }
        return result;
    }
}
